package RMI;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;


public class RemoteChat implements Chat{
	
	String chatText = "";
	private List<MessageEventListeners> listeners = new ArrayList<MessageEventListeners>();
	
	@Override
	public String sendMessage(String text) throws RemoteException {
		chatText = text;
		System.out.println(text);
		fireMessageEvent();
		return chatText;
		
	}

	@Override
	public String reviveMessage() throws RemoteException {
		return chatText;
	}
	
	public synchronized void addMessageEventListeners(MessageEventListeners l) {
		listeners.add(l);
	}
	// method to remove listeners from the bean
	public synchronized void removeMessageEventListeners(MessageEventListeners l) {
		listeners.remove(l);
	}
	public void fireMessageEvent(){
		for (MessageEventListeners l : listeners) {
			l.messageEvent(new MessageEvent(this));
		}
		
	}

	
	
	
}
